//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Chart.Serialization;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Chart
{
    public class GraphicFrame : IAnchored
    {
        private Transform2D _xfrm;
        private bool fPublished;
        private Graphic graphic;
        private GraphicalObjectData graphicData;
        private int id;

        private string macro = "";


        private string name;
        private GraphicalObjectFrameNonVisual nvGraphicFramePr;

        public Graphic Graphic
        {
            get
            {
                if (graphic == null)
                    graphic = new Graphic();
                return graphic;
            }
            set { graphic = value; }
        }

        public string Macro
        {
            get { return macro; }
            set { macro = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }


        internal GraphicalObjectData GraphicData
        {
            get { return graphicData; }
            set { graphicData = value; }
        }


        internal GraphicalObjectFrameNonVisual NonVisualGraphicFrameProperties
        {
            get
            {
                if (nvGraphicFramePr == null)
                    nvGraphicFramePr = new GraphicalObjectFrameNonVisual();
                return nvGraphicFramePr;
            }
            set { nvGraphicFramePr = value; }
        }

        public Transform2D Xfrm
        {
            get
            {
                if (_xfrm == null)
                    _xfrm = new Transform2D();
                return _xfrm;
            }
            set { _xfrm = value; }
        }

        public bool Published
        {
            get { return fPublished; }
            set { fPublished = value; }
        }

        #region IAnchored Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("graphicFrame"))
            {
                Macro = c.GetOptionalString("macro");
                fPublished = c.GetOptionalBoolean("fPublished", false);
                nvGraphicFramePr = c.ReadElement<GraphicalObjectFrameNonVisual>();
                Xfrm = c.ReadElement<Transform2D>();
                ReadContext c2 = c.SwitchNamespace(DrawingConstants.DrawingNamespaceUri, true); //\\ sure?
                if (c2.GetData<IShapeReader>() == null)
                    c2.AddData<IShapeReader>(new ShapeHelper());
                graphic = c2.ReadElement<Graphic>();
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("graphicFrame"))
            {
                c.SetOptionalString("macro", Macro);
                c.SetOptionalBoolean("fPublished", fPublished, false);
                c.WriteElement(NonVisualGraphicFrameProperties);
                c.WriteElement(Xfrm);
                WriteContext c2 = c.Switch(ChartConstants.ChartNamespace);
                c2.WriteOptionalElement(graphic);
            }
        }

        #endregion
    }
}